//
//  BKChuangJianViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/4.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKChuangJianViewController.h"

#import "BKHeZhaoViewModel.h"
#import "LoginViewController.h"
#import "BKHeZhaoStep3ViewController.h"
#import "BKSearchField.h"

@interface BKChuangJianViewController ()
@property (weak, nonatomic) IBOutlet BKTextField *xuexiao;
@property (weak, nonatomic) IBOutlet BKTextField *banji;
@property (weak, nonatomic) IBOutlet BKSearchField *year;

@property (weak, nonatomic) IBOutlet BKTextField *mima;

@end

@implementation BKChuangJianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)p_back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)p_submit:(id)sender {
    
    if ([_xuehao.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入学校" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    
    if ([_banji.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入班级" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    
    if ([_xuehao.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请学号" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    if ([_mima.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    
    NSDictionary *cookieDic = [userDefault objectForKey:@"Cookie"];
    
    if (cookieDic ==nil) {
        LoginViewController *login = [[LoginViewController alloc] initWithNibName:NSStringFromClass([LoginViewController class]) bundle:nil];
        [self presentViewController:login animated:YES completion:nil];
        
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:_params];
    params[@"xuexiao"]= _xuehao.text;
    params[@"ban"] = _banji.text;
    params[@"xuehao"] = _xuehao.text;
    params[@"pass"] = _mima.text;
    params[@"year"] = _year.text;
    
    params[@"sid"] = _params[@"sid"]==nil?@"0":_params[@"sid"];
    
    
    
    
    __weak BKChuangJianViewController *weafSelf = self;
    [BKHeZhaoViewModel chuangjianWithParams:params success:^(NSString *banjiid) {
        
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^(void) {
        
            BKHeZhaoStep3ViewController *step03 = [[BKHeZhaoStep3ViewController alloc] initWithNibName:NSStringFromClass([BKHeZhaoStep3ViewController class]) bundle:nil];
            [step03.params addEntriesFromDictionary:weafSelf.params];
            step03.isSQHiden = YES;
            step03.ban = banjiid;
            
            [self presentViewController:step03 animated:YES completion:nil];
            
        });
    } failure:^(NSString *message) {
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^(void) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alertView show];});
        
    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
